package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.jingyou.service.IJYQuestionService;
import com.xh.bussiness.tiku.bean.dto.QuestionTypeDto;
import com.xh.bussiness.tiku.bean.dto.SubjectQuestionTypeDto;
import com.xh.bussiness.tiku.bean.request.QuestionTypeQueryRequest;
import com.xh.bussiness.tiku.bean.request.QuestionTypeSaveRequest;
import com.xh.bussiness.tiku.dao.SubjectQuestionTypeMapper;
import com.xh.bussiness.tiku.model.QuestionType;
import com.xh.bussiness.tiku.dao.QuestionTypeMapper;
import com.xh.bussiness.tiku.model.SubjectQuestionType;
import com.xh.bussiness.tiku.service.IQuestionTypeService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.CacheUtils;
import com.xh.core.util.ReflectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题型 服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2019-01-14
 */
@Service
public class QuestionTypeServiceImpl extends BaseServiceImpl<QuestionTypeMapper, QuestionType> implements IQuestionTypeService {

    @Autowired
    private SubjectQuestionTypeMapper subjectQuestionTypeMapper;
    @Autowired
    private IJYQuestionService jyquestionService;
    
    @Override
    public XhPage queryQuestionTypePage(XhPage page, QuestionTypeQueryRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionType>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryQuestionType(QuestionTypeQueryRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionType>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public List queryAllQuestionType() {
        List<QuestionTypeDto> result=(List<QuestionTypeDto>)CacheUtils.get("tiku.questionType.all");
        if(result==null){
            QueryWrapper qw=new QueryWrapper<QuestionType>();
//            result=this.selectList(qw, QuestionTypeDto.class);
            result = new ArrayList<>();
            List<QuestionType> questionTypes = this.dao.selectList(qw);
            for (QuestionType questionType : questionTypes){
                QuestionTypeDto questionTypeDto = new QuestionTypeDto();
                questionTypeDto.setId(questionType.getId());
                questionTypeDto.setName(questionType.getName());
                result.add(questionTypeDto);
            }
            CacheUtils.set("tiku.questionType.all",result);
        }
        return result;
    }

    @Override
    public List queryQuestionTypeBySubjectId(String subjectId) {
        List<QuestionTypeDto> result=(List<QuestionTypeDto>)CacheUtils.get("tiku.questionTypeBySubjectId."+subjectId);
        if(result==null){
            List list=this.dao.queryListBySubjectId(subjectId);
            if(list==null||list.size()==0){
                result = queryAllQuestionType();
            }else {
                result = ReflectUtils.convertUnderlinMapList2List(list, QuestionTypeDto.class);
            }
            CacheUtils.set("tiku.questionTypeBySubjectId."+subjectId,result);
        }
        return result;
    }

    @Override
    public SubjectQuestionTypeDto saveSubjectQuestionTypes(QuestionTypeSaveRequest bean) {
        QueryWrapper qw=new QueryWrapper<SubjectQuestionType>();
        qw.eq("subjectId",bean.getSubjectId());
        this.subjectQuestionTypeMapper.delete(qw);
        SubjectQuestionTypeDto subjectQuestionTypeDto=new SubjectQuestionTypeDto();
        subjectQuestionTypeDto.setSubjectId(bean.getSubjectId());
        subjectQuestionTypeDto.setQuestionTypes(new ArrayList<>());

        for (Integer questionTypeId:bean.getQuestionTypeIds()){
            QuestionTypeDto questionTypeDto=new QuestionTypeDto();
            questionTypeDto.setId(questionTypeId);

            SubjectQuestionType subjectQuestionType=new SubjectQuestionType();
            subjectQuestionType.setSubjectId(bean.getSubjectId());
            subjectQuestionType.setQuestionTypeId(questionTypeId);
            this.subjectQuestionTypeMapper.insert(subjectQuestionType);
        }
        return null;
    }

    @Override
    public String deleteSubjectQuestionTypes(String subjectId, Integer questionTypeId) {
        return null;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(QuestionTypeQueryRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }

	@Override
	public List<QuestionType> queryBySubject(String subject) throws Exception {
		
		return jyquestionService.queryQuestionType(subject);
	}

    /**
     * 给复合题小题查询题型
     * @author 赵文博
     * @date 2021/7/7 17:00
     * @return java.util.List<com.xh.bussiness.tiku.model.QuestionType>
     **/
    @Override
    public List<QuestionType> queryById() {
//        QueryWrapper<QuestionType> queryWrapper = new QueryWrapper();
//        queryWrapper.in("id", Arrays.asList("1","2","3","4","5"));
//        List<QuestionType> list = this.selectList(queryWrapper,QuestionType.class);
        return this.dao.queryById();
    }
}
